package com.example.module_motocircle.ui.post

import androidx.lifecycle.MutableLiveData
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.RequestEvent
import com.example.lib_common.base.state.Success
import com.example.module_motocircle.model.PostItem
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import javax.inject.Inject

@HiltViewModel
class PostListViewModel @Inject constructor(private val repository: PostListRepository) :
    BaseViewModel() {
    /*
     获得帖子数据
     * stateFlow:是一种特殊的sharedFlow,具有粘性 replay = 1，必须有初始值（sharedFlow默认0）
     */
    private val mPostState_ = MutableStateFlow<RequestEvent>(Loading)
    val mPostState = mPostState_.asStateFlow()

    var mPostListData = listOf<PostItem>()

    val mCheckIfHasFocus by lazy {
        MutableLiveData<Boolean>()
    }

    fun checkIfHasFocus() {
        execute(
            action = repository.checkIfHasFocus(),
            onError = {
                mCheckIfHasFocus.value = false
            },
            onFinish = {
                mCheckIfHasFocus.value = it.data
            }
        )
    }

    //分页参数
    private var sortBy: String? = null
    private var keyword: String? = null
    private var memberId: String? = null
    private var ifFocus: Int? = null
    private var topicId: String? = null

    private var pageNum = 1
    private var pageSize = 10

    fun setPageParam(
        sortBy: String?,
        keyword: String?,
        memberId: String?,
        ifFocus: Int?,
        topicId: String?
    ) {
        this.sortBy = sortBy
        this.keyword = keyword
        this.memberId = memberId
        this.ifFocus = ifFocus
        this.topicId = topicId
    }

    /**
     * 帖子列表
     */
    fun pageList() {
        execute(
            action = repository.pageList(
                sortBy,
                topicId,
                memberId,
                ifFocus,
                pageNum,
                pageSize,
                keyword
            ),
            onStart = {
                mPostState_.value = Loading
            },
            onFinish = {
                if (it.data.records.isNullOrEmpty()) {//空数据
                    mPostState_.value = Empty
                } else {
                    mPostListData = it.data.records!! //先数据后状态
                    mPostState_.value = Success
                }
            },
            onError = {
                mPostState_.value = Failed(it)
            }
        )
    }

    /*
    帖子点赞和取消
     */
    private val requestLikeEvent = MutableSharedFlow<Int>()//下标
    val requestLikeEvent_ = requestLikeEvent.asSharedFlow()

    /*
         * 点赞帖子
     */
    fun giveLike(postsId: String, position: Int) {
        execute(
            action = repository.giveLike(postsId),
            onFinish = {
                if (it.data) {
                    requestLikeEvent.emit(position)
                } else {
                    requestLikeEvent.emit(-1)
                }

            },
            onError = {
                requestLikeEvent.emit(-1)
            }

        )
    }
}